package com.learning.JavaSE.workExercise.module4.Task3;

/**
 * 火车买票案例 (两种实现方式) : 共100张票,三个线程一起卖票,不允许出现出现负票和多卖的现象  (描述设置锁的时候要注意的事项:(Thread 和Runnable)
 */
public class TrainTicket {

     private static int tickets = 100;
     public static void main(String[] args) {
          Runnable ticketSeller = () -> {
               for (int i = 0; i < 50; i++) {
                    sellTicket();
               }
          };
          Thread thread1 = new Thread(ticketSeller);
          Thread thread2 = new Thread(ticketSeller);
          Thread thread3 = new Thread(ticketSeller);

          thread1.start();
          thread2.start();
          thread3.start();
     }

     private static void sellTicket() {
          if (tickets > 0) {
               System.out.println( Thread.currentThread().getName()+ "卖出了一张票，剩余：" + --tickets);
          } else {
               System.out.println("没有票了!");
          }
     }
}